package net.wanho.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import lombok.RequiredArgsConstructor;
import net.wanho.mapper.StatisticsMapper;
import net.wanho.service.StatisticsService;
import net.wanho.vo.BuyDailyVO;
import net.wanho.vo.BuyListTypeVO;
import net.wanho.vo.InOutMonthVO;
import net.wanho.vo.LiveDataVO;
import net.wanho.vo.StoreInventVO;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 这个类是：
 *
 * @author: MENG HU WANG
 * @date: 2025/2/7 14:20
 * @version: 1.0
 */
@Service
@RequiredArgsConstructor
public class StatisticsServiceImpl implements StatisticsService {
    private final StatisticsMapper statisticsMapper;

    @Override
    public List<StoreInventVO> storeInvent() {

        return statisticsMapper.storeInvent();
    }

    @Override
    public BuyDailyVO getBuyListDaily() {

        return statisticsMapper.selectBuyListDaily();
    }

    @Override
    public List<InOutMonthVO> getInOutMonth() {
        // 获取2024年的入库和出库数据
        List<Map<String, Object>> monthlyData = statisticsMapper.selectMonthlyInOut();

        // 如果没有数据，返回空列表
        if (CollectionUtils.isEmpty(monthlyData)) {
            return new ArrayList<>();
        }

        // 转换数据
        return monthlyData.stream()
                .map(data -> InOutMonthVO.builder()
                        .month(data.get("month").toString())
                        .in(Integer.valueOf(data.get("inCount").toString()))
                        .out(Integer.valueOf(data.get("outCount").toString()))
                        .build())
                .collect(Collectors.toList());
    }

    @Override
    public BuyListTypeVO getBuyListType() {
        return statisticsMapper.selectBuyListType();
    }

    @Override
    public LiveDataVO getLiveData() {
        return statisticsMapper.selectTodayData();
    }
}
